<template>
  <el-dialog v-el-drag-dialog :title="!form.id?'新建会员等级':'编辑会员等级'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="memberForm" :model="form" :rules="rules" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.id" hidden/>
        </el-form-item>
        <el-form-item label="等级名称" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="所需成长值" prop="growthPoint">
          <el-input-number v-model="form.growthPoint" :min="0" />
        </el-form-item>
        <el-form-item label="默认等级" prop="defaultStatus">
          <el-radio v-model="form.defaultStatus" label="0">是</el-radio>
          <el-radio v-model="form.defaultStatus" label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="免运费标准" prop="freeFreightPoint">
          <el-input-number v-model="form.freeFreightPoint" :min="0" />
        </el-form-item>
        <el-form-item label="每次评价获取的成长值" prop="commentGrowthPoint">
          <el-input-number v-model="form.commentGrowthPoint" :min="0" />
        </el-form-item>
        <el-form-item label="免邮特权" prop="privilegeFreeFreight">
          <el-radio v-model="form.privilegeFreeFreight" label="0">是</el-radio>
          <el-radio v-model="form.privilegeFreeFreight" label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="价格特权" prop="privilegeMemberPrice">
          <el-radio v-model="form.privilegeMemberPrice" label="0">是</el-radio>
          <el-radio v-model="form.privilegeMemberPrice" label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="生日特权" prop="privilegeBirthday">
          <el-radio v-model="form.privilegeBirthday" label="0">是</el-radio>
          <el-radio v-model="form.privilegeBirthday" label="1">否</el-radio>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { memberLevelLoad, memberLevelSave } from '@/api/system/member'

export default {
  name: 'LevelFormDialog',
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        id: undefined,
        name: undefined,
        growthPoint: undefined,
        defaultStatus: '1',
        freeFreightPoint: undefined,
        commentGrowthPoint: undefined,
        privilegeFreeFreight: '1',
        privilegeMemberPrice: '1',
        privilegeBirthday: '1'
      },
      rules: {
        name: [{ required: true, message: '等级名称不能为空' }],
        growthPoint: [{ required: true, message: '所需成长值不能为空' }],
        freeFreightPoint: [{ required: true, message: '免运费标准不能为空' }],
        commentGrowthPoint: [{ required: true, message: '所需成长值不能为空' }]
      },
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
      })
    },
    loadForm(id) {
      memberLevelLoad({ id: id }).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['system:member:edit'])
      }).catch(() => {
      })
    },
    onSubmit() {
      this.$refs.memberForm.validate(valid => {
        if (valid) {
          memberLevelSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.memberForm.resetFields()
      this.visible = false
    }
  }
}
</script>
